package com.yndsoft.jc.supplier.order;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.supplier.adapter.TabPagerAdapter;
import com.yndsoft.jc.supplier.base.SupplierBaseFragment;
import com.yndsoft.jc.supplier.base.SupplierBaseFragmentActivity;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.DensityUtil;
import com.yndsoft.jc.supplier.utils.ScreenUtil;
import com.yndsoft.jc.supplier.widget.imageselector.utils.LogUtils;
import com.yndsoft.jc.supplier.widget.tabs.TabView;
import com.yndsoft.jc.supplier.widget.tabs.Tabs;

import java.util.ArrayList;
import java.util.List;

public class OrderActivity extends SupplierBaseFragmentActivity implements View.OnClickListener {

    private Tabs supplierTabs;
    private RelativeLayout supplierIndicator;
    private View indicator;
    private ViewPager supplierPager;
    private OrderFragment allFragment;
    private OrderFragment waitFragment;
    private OrderFragment finishFragment;

    @Override
    public int getContentResId() {
        return R.layout.activity_order;
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, OrderActivity.class));
    }

    @Override
    public void initView() {
        ImageButton barBack = (ImageButton) findViewById(R.id.bar_back);
        TextView barTitle = (TextView) findViewById(R.id.bar_title);
        supplierTabs = (Tabs) findViewById(R.id.supplier_tabs);
        supplierIndicator = (RelativeLayout) findViewById(R.id.supplier_indicator);
        indicator = findViewById(R.id.indicator);
        supplierPager = (ViewPager) findViewById(R.id.supplier_pager);

        barTitle.setText("订单列表");
        barBack.setOnClickListener(this);
    }

    @Override
    public void initData() {
        List<SupplierBaseFragment> fragmentList = new ArrayList<>();
        fragmentList.clear();
        allFragment = OrderFragment.newInstance(Constants.ALL);
        waitFragment = OrderFragment.newInstance(Constants.WAIT);
        finishFragment = OrderFragment.newInstance(Constants.FINISH);
        fragmentList.add(allFragment);
        fragmentList.add(waitFragment);
        fragmentList.add(finishFragment);

        supplierPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager(), fragmentList));

        supplierPager.setOnPageChangeListener(pageChangeListener);
        supplierPager.setOffscreenPageLimit(fragmentList.size() - 1);

        supplierTabs.setViewPager(supplierPager);

        supplierTabs.getTabView(0).removeShow();
        supplierTabs.getTabView(1).removeShow();
        supplierTabs.getTabView(2).removeShow();

        /**初始化指示器宽度*/
        supplierIndicator.getLayoutParams().width = ScreenUtil.getScreenWidth(mContext) / fragmentList.size();
        supplierIndicator.requestLayout();
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        );
        int interval = DensityUtil.dp2px(mContext, 38);
        params.setMargins(interval, 0, interval, 0);
        indicator.setLayoutParams(params);
    }

    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float offsetX = positionOffset * supplierIndicator.getWidth();
            int startX = position * supplierIndicator.getWidth();
            float translationX = startX + offsetX;
            ViewCompat.setTranslationX(supplierIndicator, translationX);

//            switch (position) {
//                case 0:
//                    supplierTabs.getCurrentItemView().removeShow();
//                    break;
//                case 1:
//                    supplierTabs.getCurrentItemView().removeShow();
//                    break;
//                case 2:
//                    supplierTabs.getCurrentItemView().removeShow();
//                    break;
//            }

//            if (0 == position) {
//                alphaTabsIndicator.getTabView(0).showNumber(alphaTabsIndicator.getTabView(0).getBadgeNumber() - 1);
//            } else if (2 == position) {
//                alphaTabsIndicator.getCurrentItemView().removeShow();
//            } else if (3 == position) {
//                alphaTabsIndicator.removeAllBadge();
//            }
        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    if (allFragment != null) {
                        allFragment.setCurrentPageIndex(0);
                        allFragment.restartLoadData();
                    }
                    break;
                case 1:
                    if (waitFragment != null) {
                        waitFragment.setCurrentPageIndex(1);
                        waitFragment.restartLoadData();
                    }
                    break;
                case 2:
                    if (finishFragment != null) {
                        finishFragment.setCurrentPageIndex(2);
                    }
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
        }
    }
}